﻿using System;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.plsql
{
	// Token: 0x02000233 RID: 563
	public class BeginAndEndProcessor : AbstractProcessor
	{
		// Token: 0x060012D0 RID: 4816 RVA: 0x0031AB7C File Offset: 0x00318D7C
		public override void process(TParseTreeNode nodeArg)
		{
			TBlockSqlStatement tblockSqlStatement = nodeArg as TBlockSqlStatement;
			if (tblockSqlStatement.GSharpSqlParser.DbVendor == EDbVendor.dbvmssql)
			{
				return;
			}
			TSourceToken tsourceToken = SourceTokenSearcher.firstSelectedToken(tblockSqlStatement.startToken.container, tblockSqlStatement.startToken.posinlist, tblockSqlStatement.endToken.posinlist, "begin");
			int num = SourceTokenOperator.curIndentLenVT(SourceTokenSearcher.firstNotWhitespaceAndReturnToken(tblockSqlStatement.startToken.container, tblockSqlStatement.startToken.posinlist, tblockSqlStatement.endToken.posinlist));
			if (this.Option.beStyleBlockLeftBEOnNewline && tsourceToken != null)
			{
				SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
				SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(num + this.Option.beStyleBlockLeftBEIndentSize));
			}
			TSourceToken tsourceToken2 = SourceTokenSearcher.lastSelectedToken(tblockSqlStatement.endToken.container, tblockSqlStatement.startToken.posinlist, tblockSqlStatement.endToken.posinlist, "end");
			if (tsourceToken2 != null)
			{
				SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createReturnSourceToken());
				int num2 = num;
				if (this.Option.beStyleBlockLeftBEOnNewline)
				{
					num2 += this.Option.beStyleBlockLeftBEIndentSize;
				}
				SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(num2));
			}
			if (tblockSqlStatement is TCommonBlock)
			{
				TCommonBlock tcommonBlock = (TCommonBlock)tblockSqlStatement;
				if (tcommonBlock.ExceptionClause != null)
				{
					TSourceToken startToken = tcommonBlock.ExceptionClause.startToken;
					SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createReturnSourceToken());
					int num3 = num;
					if (this.Option.beStyleBlockLeftBEOnNewline)
					{
						num3 += this.Option.beStyleBlockLeftBEIndentSize;
					}
					SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createWhitespaceSourceToken(num3));
					TExceptionHandlerList handlers = tcommonBlock.ExceptionClause.Handlers;
					if (handlers != null && handlers.size() > 0)
					{
						this.handleExceptionHandlers(startToken, handlers);
					}
				}
			}
		}

		// Token: 0x060012D1 RID: 4817 RVA: 0x0031AD40 File Offset: 0x00318F40
		private void handleExceptionHandlers(TSourceToken exceptionToken, TExceptionHandlerList handlers)
		{
			for (int i = 0; i < handlers.size(); i++)
			{
				TExceptionHandler exceptionHandler = handlers.getExceptionHandler(i);
				if (exceptionHandler.Statements != null && exceptionHandler.Statements.size() == 1)
				{
					TSourceToken startToken = exceptionHandler.Statements.get(0).startToken;
					TSourceToken tsourceToken = SourceTokenSearcher.backforwardSearch(startToken, 5, "then");
					if (tsourceToken != null)
					{
						this.wrapStatement(exceptionToken, startToken, tsourceToken, exceptionHandler.startToken);
					}
				}
			}
		}

		// Token: 0x060012D2 RID: 4818 RVA: 0x0031ADB0 File Offset: 0x00318FB0
		private void wrapStatement(TSourceToken exceptionToken, TSourceToken startToken, TSourceToken thenToken, TSourceToken whenToken)
		{
			int num = SourceTokenOperator.curColumnNumberVT(exceptionToken);
			num += this.Option.indentLen;
			SourceTokenOperator.addBefore(this.Option, thenToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, thenToken, SourceTokenOperator.createWhitespaceSourceToken(num));
			SourceTokenOperator.addBefore(this.Option, whenToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, whenToken, SourceTokenOperator.createWhitespaceSourceToken(num));
			num += this.Option.indentLen;
			SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createWhitespaceSourceToken(num));
		}
	}
}
